/*
 * Copyright 2012 Faculty of Informatics - Masaryk University.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package cz.muni.fi.pa165.creatures.model;

import java.io.Serializable;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.IdClass;
import javax.persistence.ManyToOne;
import javax.persistence.PrimaryKeyJoinColumn;
import javax.persistence.Table;

/**
 * This association class represents relationship between {@link Weapon} and
 * {@link CreatureType} where effectivity of that weapon against particular
 * creature type is taken into consideration. This relationship is of the kind
 * "many-to-many".
 *
 * @author xmiklos1
 */
@Entity
@Table(name = "CREATURE_TYPE_WEAPON")
@IdClass(CreatureTypeWeaponId.class)
public class CreatureTypeWeapon implements Serializable {

    private static final long serialVersionUID = -8358289624723505294L;

    /**
     * Type of creature in {@code CreatureTypeWeapon} relationship.
     */
    @Id
    @ManyToOne(cascade = {CascadeType.PERSIST, CascadeType.MERGE})
    @PrimaryKeyJoinColumn(name = "CREATURE_TYPE_ID", referencedColumnName = "id")
    private CreatureType creatureType;

    /**
     * Weapon in {@code CreatureTypeWeapon} relationship.
     */
    @Id
    @ManyToOne(cascade = {CascadeType.PERSIST, CascadeType.MERGE})
    @PrimaryKeyJoinColumn(name = "WEAPON_ID", referencedColumnName = "id")
    private Weapon weapon;

    /**
     * Effectivity of some particular {@link Weapon} against some
     * {@link CreatureType}.
     */
    @Column(name = "EFFECTIVITY")
    private Float effectivity;

    public CreatureTypeWeapon() {
    }

    public CreatureType getCreatureType() {
        return creatureType;
    }

    public void setCreatureType(CreatureType creatureType) {
        this.creatureType = creatureType;
    }

    public Weapon getWeapon() {
        return weapon;
    }

    public void setWeapon(Weapon weapon) {
        this.weapon = weapon;
    }

    public Float getEffectivity() {
        return effectivity;
    }

    /**
     * Sets the effectivity of some {@link Weapon} against particular
     * {@link CreatureType}.
     *
     * @param effectivity effectivity of some weapon against particular creature
     */
    public void setEffectivity(Float effectivity) {
        this.effectivity = effectivity;
    }

    @Override
    public int hashCode() {
        int hash = 7;
        hash = 89 * hash + (this.creatureType != null ? this.creatureType.hashCode() : 0);
        hash = 89 * hash + (this.weapon != null ? this.weapon.hashCode() : 0);
        return hash;
    }

    @Override
    public boolean equals(Object obj) {
        if (obj == null) {
            return false;
        }
        if (getClass() != obj.getClass()) {
            return false;
        }
        final CreatureTypeWeapon other = (CreatureTypeWeapon) obj;
        if (this.creatureType != other.creatureType && (this.creatureType == null
                || !this.creatureType.equals(other.creatureType))) {
            return false;
        }
        if (this.weapon != other.weapon && (this.weapon == null
                || !this.weapon.equals(other.weapon))) {
            return false;
        }
        return true;
    }
}
